<?php

namespace App\Server\Request;

class Request
{
    public $apiLink = "";

    public function fetchApi(
        string $url,
        array $params = [],
        string $method = 'POST',
        string $type = "normal",
    ) {
        $url = $this->apiLink . $url;
        $request = \App\Server\Util\GuzzleRequest::{$type}($url, $params, $method);
        return $this->retDecorate($request);
    }
    
    public function retDecorate($request)
    {
        if(empty($request) || empty($request['result']) || $request['code'] !== 0)
        {
            [$errMsg, $errCode] = config('api.requestErr');
            $message = $request['message'] ?? $errMsg;
            $code = $request['code'] ?? $request['code'];
            throw new \Exception(json_encode([
                'error', $message, $code
            ]));
        }
        return $request['result'];
    }

}